<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：User.class.php
 * @类功能: 用户控制器
 * @开发者: 陈旭林
 * @开发时间： 2017-09-01
 * @版本：version 1.0
 */

namespace app\api\controller;

use app\common\controller\ApiBaseController;
use app\common\enum\HtmlEnumValue;
use app\common\enum\IsDelete;
use app\common\enum\IsHistory;
use app\common\enum\IsPolicy;
use app\common\enum\PolicyStatus;
use app\common\ext\IDb;
use app\common\ext\User;

class UserPolicy extends ApiBaseController{

    /**
     * @功能：个人保单列表
     * @开发者：cxl
     */
    public function policy_list(){
        //接收参数
        $user_id = self::$uid;

        //判断保单是否存在
        $user_policy_where['is_delete'] = IsDelete::No;
        $user_policy_where['is_history'] = IsHistory::not;
        $user_policy_where['user_id'] = $user_id;
        $user_policy_list = IDb::getInstance("asset_policy")->setDbWhere($user_policy_where)->setDbOrder("user_policy_id")->sel();
        if(empty($user_policy_list) || !is_array($user_policy_list)){
            $user_policy_list = array();
        }

        foreach ($user_policy_list as &$data){
            if(isset(HtmlEnumValue::$enum_value['pay_type'][$data['pay_type']])){
                $data['pay_type_name'] = HtmlEnumValue::$enum_value['pay_type'][$data['pay_type']];
            }
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("user_policy_list",$user_policy_list);
        self::send();
    }

    /**
     * @功能：添加个人保单
     * @开发者：cxl
     */
    public function policy_add(){
        //接收参数
        $user_id = self::$uid;
        $company_name  = self::get_data('company_name',self::NOT_EMPTY,"保险公司名称不能为空！");
        $policy_type  = self::get_data('policy_type',self::NOT_EMPTY,"保单类型不能为空！");
        $applicant_name  = self::get_data('applicant_name',self::NOT_EMPTY,"投保人不能为空！");
        $pay_type  = self::get_data('pay_type',self::NOT_EMPTY,"缴纳方式不能为空！");
        $pay_money  = self::get_data('pay_money',self::INT,"缴纳金额不能为空！");
        $start_time  = self::get_data('start_time',self::NOT_EMPTY,"生效日期不能为空！");
        $img_url  = self::get_data('img_url');

        //长度限制
        $pay_money = self::data_filter($pay_money,self::INT,'缴纳金额');

        //判断是否设置空值
        if(trim($pay_money) == "") $pay_money = null;
        if(trim($start_time) == "") $start_time = null;
        if(trim($img_url) == "") $img_url = null;

        //添加保单
        $user_policy_data['user_id'] = $user_id;
        $user_policy_data['company_name'] = $company_name;
        $user_policy_data['policy_type'] = $policy_type;
        $user_policy_data['applicant_name'] = $applicant_name;
        $user_policy_data['pay_type'] = $pay_type;
        $user_policy_data['pay_money'] = $pay_money;
        $user_policy_data['start_time'] = $start_time;
        $user_policy_data['img_url'] = $img_url;
        $user_policy_data['policy_status'] = PolicyStatus::Wait;
        $user_policy_data['is_history'] = IsHistory::not;
        $user_policy_data['create_time'] = get_date_time();

        //开启事物
        IDb::dbStartTrans();

        //取得保单列表
        $user_policy_add = IDb::getInstance("asset_policy")->setDbData($user_policy_data)->add();

        //判断添加保单是否正常
        if($user_policy_add === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0001);
            self::set_msg("添加保单信息失败！");
            self::send();
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_POLICY,IsPolicy::Yes);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0002);
            self::set_msg("添加保单信息失败！");
            self::send();
        }

        //事物提交
        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("user_policy_id",$user_policy_add);
        self::send();
    }

    /**
     * @功能：编辑个人保单
     * @开发者：cxl
     */
    public function policy_edit(){
        //接收参数
        $user_id = self::$uid;
        $user_policy_id  = self::get_data('user_policy_id',self::NOT_EMPTY,"保单编码不能为空！");
        $company_name  = self::get_data('company_name',self::NOT_EMPTY,"保险公司名称不能为空！");
        $policy_type  = self::get_data('policy_type',self::NOT_EMPTY,"保单类型不能为空！");
        $applicant_name  = self::get_data('applicant_name',self::NOT_EMPTY,"投保人不能为空！");
        $pay_type  = self::get_data('pay_type',self::NOT_EMPTY,"缴纳方式不能为空！");
        $pay_money  = self::get_data('pay_money',self::INT,"缴纳金额不能为空！");
        $start_time  = self::get_data('start_time',self::NOT_EMPTY,"生效日期不能为空！");
        $img_url  = self::get_data('img_url');

        //长度限制
        $pay_money = self::data_filter($pay_money,self::INT,'缴纳金额');

        //判断是否设置空值
        if(trim($pay_money) == "") $pay_money = null;
        if(trim($start_time) == "") $start_time = null;
        if(trim($img_url) == "") $img_url = null;

        //取得保单列表
        $user_policy_where['user_policy_id'] = $user_policy_id;
        $user_policy_info = IDb::getInstance("asset_policy")->setDbWhere($user_policy_where)->row();

        //判断是否审核通过
        if(empty($user_policy_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("修改保单信息失败！");
            self::send();
        }

        //开启事物
        IDb::dbStartTrans();

        $user_policy_data['company_name'] = $company_name;
        $user_policy_data['policy_type'] = $policy_type;
        $user_policy_data['applicant_name'] = $applicant_name;
        $user_policy_data['pay_type'] = $pay_type;
        $user_policy_data['pay_money'] = $pay_money;
        $user_policy_data['start_time'] = $start_time;
        $user_policy_data['img_url'] = $img_url;

        //判断是否审核通过
        if($user_policy_info['policy_status'] == PolicyStatus::OK) {
            //取得保单列表
            $policy_data = null;
            $user_policy_where = null;
            $user_policy_where['user_id'] = $user_id;
            $user_policy_where['user_policy_id'] = $user_policy_id;
            $policy_data['is_history'] = IsHistory::yes;
            $user_policy_upd = IDb::getInstance("asset_policy")->setDbWhere($user_policy_where)->setDbData($policy_data)->upd();
            if($user_policy_upd === false){
                //数据回滚
                IDb::dbRollback();

                //返回错误
                self::set_code(self::WARNING_0002);
                self::set_msg("修改保单信息失败！");
                self::send();
            }

            //添加保单
            $user_policy_data['user_id'] = $user_id;
            $user_policy_data['policy_status'] = PolicyStatus::Wait;
            $user_policy_data['is_history'] = IsHistory::not;
            $user_policy_data['create_time'] = get_date_time();
            $user_policy_add = IDb::getInstance("asset_policy")->setDbData($user_policy_data)->add();
            if($user_policy_add === false){
                //数据回滚
                IDb::dbRollback();

                //返回错误
                self::set_code(self::WARNING_0003);
                self::set_msg("修改保单信息失败！");
                self::send();
            }
        }else {
            //添加保单
            $user_policy_where = null;
            $user_policy_data['policy_status'] = PolicyStatus::Wait;
            $user_policy_data['is_history'] = IsHistory::not;
            $user_policy_data['create_time'] = get_date_time();
            $user_policy_where['user_id'] = $user_id;
            $user_policy_where['user_policy_id'] = $user_policy_id;
            $user_policy_upd = IDb::getInstance("asset_policy")->setDbWhere($user_policy_where)->setDbData($user_policy_data)->upd();

            //判断修改保单是否正常
            if ($user_policy_upd === false) {
                IDb::dbRollback();
                self::set_code(self::WARNING_0004);
                self::set_msg("修改保单信息失败！");
                self::send();
            }
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_POLICY,IsPolicy::Yes);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0005);
            self::set_msg("修改保单信息失败！");
            self::send();
        }

        //事物提交
        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：删除个人保单
     * @开发者：cxl
     */
    public function policy_del(){
        //接收参数
        $user_id = self::$uid;
        $user_policy_id  = self::get_data('user_policy_id',self::NOT_EMPTY,"保单型号编码不能为空！");

        //添加保单
        $user_policy_data['is_delete'] = IsDelete::Yes;
        $user_policy_data['update_time'] = get_date_time();

        //取得保单列表
        $user_policy_where['user_id'] = $user_id;
        $user_policy_where['user_policy_id'] = $user_policy_id;

        IDb::dbStartTrans();
        $user_policy_upd = IDb::getInstance("asset_policy")->setDbWhere($user_policy_where)->setDbData($user_policy_data)->upd();

        //判断修改保单是否正常
        if($user_policy_upd === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0002);
            self::set_msg("删除保单信息失败！");
            self::send();
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_POLICY);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0003);
            self::set_msg("同步保单信息失败！");
            self::send();
        }

        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }
}
